package com.yanqu.road.entity.activity.flowerstudy.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.flowerstudy.eFlowerStudyPeriodType;
import com.yanqu.road.utils.string.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyConfig {

    private ActivityInfo activityInfo;

    private Map<Integer,FlowerStudyVisitConfig> visitConfigMap = new ConcurrentHashMap<>();

    private Map<Integer,FlowerStudyPoemConfig> poemConfigMap  = new ConcurrentHashMap<>();

    private Map<Integer,FlowerStudyExamConfig> examConfigMap  = new ConcurrentHashMap<>();

    private List<FlowerStudyTitleConfig> titleConfigList  = new ArrayList<>();

    private Map<Integer, FlowerStudyExamQuestionConfig> studyExamQuestionConfigMap  = new ConcurrentHashMap<>();

    Map<Integer,Map<Integer,FlowerStudySessionRewardConfig>> sessionRewardConfigMap = new ConcurrentHashMap<>();

    private List<Integer> headIconList = new ArrayList<>();




    //诗书问答开始时间
    private String poemBeginTime;
    //诗书问答结束时间
    private String poemEndTime;
    //诗书问答一天的题目数量
    private int poemQANum;
    //一考到底开始时间
    private String examBeginTime;
    //一考到底结束时间
    private String examEndTime;

    //一考到底开始date
    private Date examBeginDate;
    //一考到底结束date
    private Date examEndDate;

    //(一考到底)一个考场匹配人数
    private int examRoomPlayerNum;
    //(一考到底)乡试题目数量
    private int localExamQuestionNum;
    //(一考到底)省试题目数量
    private int provincialExamQuestionNum;
    //(一考到底)乡试题目数量
    private int palaceExamQuestionNum;
    //(一考到底)AI正确率
    private int aiAccuracy;
    //(一考到底)答题时间
    private int answerTime;
    //(一考到底)判断时间
    private int determineTime;
    //(一考到底)答对一题获得的积分
    private int examOneQuestionScore;
    //(一考到底)乡试开始时间
    private String localExamBeginTime;
    //(一考到底)乡试结束时间
    private String localExamEndTime;
    //(一考到底)乡试开始date
    private Date localExamBeginDate;
    //(一考到底)乡试结束date
    private Date localExamEndDate;

    //(一考到底)省试开始时间
    private String provincialExamBeginTime;
    //(一考到底)省试结束时间
    private String provincialExamEndTime;
    //(一考到底)省试开始date
    private Date provincialExamBeginDate;
    //(一考到底)省试结束date
    private Date provincialExamEndDate;


    //(一考到底)殿试开始时间
    private String palaceExamBeginTime;
    //(一考到底)殿试结束时间
    private String palaceExamEndTime;
    //(一考到底)殿试开始date
    private Date palaceExamBeginDate;
    //(一考到底)殿试结束date
    private Date palaceExamEndDate;



    //加入ai时间
    private int joinAiTime;

    //(一考到底)省式排行榜资格
    private int provincialExamNeedRanking;
    //(一考到底)殿试排行榜资格
    private int palaceExamNeedRanking;
    //(一考到底)乡试可参与次数
    private int localExamCanJoinNum;
    //(一考到底)省试可参与次数
    private int provincialExamCanJoinNum;
    //(一考到底)殿试可参与次数
    private int palaceExamCanJoinNum;

    private String shareReward;

    private String mailTitle;

    private String mailContent;

    private int sitDownRate;

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public Map<Integer, FlowerStudyVisitConfig> getVisitConfigMap() {
        return visitConfigMap;
    }

    public Map<Integer, FlowerStudyPoemConfig> getPoemConfigMap() {
        return poemConfigMap;
    }

    public Map<Integer, FlowerStudyExamConfig> getExamConfigMap() {
        return examConfigMap;
    }

    public List<FlowerStudyTitleConfig> getTitleConfigList() {
        return titleConfigList;
    }

    public Map<Integer, FlowerStudyExamQuestionConfig> getStudyExamQuestionConfigMap() {
        return studyExamQuestionConfigMap;
    }

    public Map<Integer, Map<Integer, FlowerStudySessionRewardConfig>> getSessionRewardConfigMap() {
        return sessionRewardConfigMap;
    }

    public int getJoinAiTime() {
        return joinAiTime;
    }

    public String getShareReward() {
        return shareReward;
    }

    public String getMailTitle() {
        return mailTitle;
    }

    public String getMailContent() {
        return mailContent;
    }

    public FlowerStudyConfig(ActivityInfo activityInfo, Map<Integer, FlowerStudyVisitConfig> visitConfigMap
            , Map<Integer, FlowerStudyPoemConfig> poemConfigMap, Map<Integer, FlowerStudyExamConfig> examConfigMap
            , List<FlowerStudyTitleConfig> titleConfigList, Map<Integer, FlowerStudyExamQuestionConfig> studyExamQuestionConfigMap
            , Map<Integer,Map<Integer,FlowerStudySessionRewardConfig>> sessionRewardConfigMap
            , Map<String, ActivityConfig> activityConfigMap) {
        this.activityInfo = activityInfo;
        this.visitConfigMap = visitConfigMap;
        this.poemConfigMap = poemConfigMap;
        this.examConfigMap = examConfigMap;
        this.titleConfigList = titleConfigList;
        this.studyExamQuestionConfigMap = studyExamQuestionConfigMap;
        this.sessionRewardConfigMap = sessionRewardConfigMap;

        this.poemBeginTime = activityConfigMap.get("STUDY_ROOM_POEM_BEGIN_TIME").getValue();
        this.poemEndTime = activityConfigMap.get("STUDY_ROOM_POEM_END_TIME").getValue();
        this.poemQANum = activityConfigMap.get("STUDY_ROOM_POEM_QA_NUMBER").getIntValue();
        this.examBeginTime = activityConfigMap.get("STUDY_ROOM_EXAM_BEGIN_TIME").getValue();
        this.examEndTime = activityConfigMap.get("STUDY_ROOM_EXAM_END_TIME").getValue();
        this.examRoomPlayerNum = activityConfigMap.get("STUDY_ROOM_EXAM_NUMBER_OF_EXAMINESS").getIntValue();
        this.localExamQuestionNum = activityConfigMap.get("STUDY_ROOM_EXAM_LOCAL_EXAMINATION_MAX_NUMBER").getIntValue();
        this.provincialExamQuestionNum = activityConfigMap.get("STUDY_ROOM_EXAM_PROVINCIAL_EXAMINATION_MAX_NUMBER").getIntValue();
        this.palaceExamQuestionNum = activityConfigMap.get("STUDY_ROOM_EXAM_PALACE_EXAMINATION_MAX_NUMBER").getIntValue();
        this.aiAccuracy = activityConfigMap.get("STUDY_ROOM_EXAM_AI_FREE_MOVE_PROBABILITY").getIntValue();
        this.answerTime = activityConfigMap.get("STUDY_ROOM_EXAM_ANSWER_TIME").getIntValue();
        this.determineTime = activityConfigMap.get("STUDY_ROOM_EXAM_DETERMINE_TIME").getIntValue();
        this.examOneQuestionScore = activityConfigMap.get("STUDY_ROOM_EXAM_CORRECT_ANSWER_SCORE").getIntValue();
        this.localExamBeginTime = activityConfigMap.get("STUDY_ROOM_EXAM_ONE_BEGIN_TIME").getValue();
        this.localExamEndTime = activityConfigMap.get("STUDY_ROOM_EXAM_ONE_END_TIME").getValue();
        this.provincialExamBeginTime = activityConfigMap.get("STUDY_ROOM_EXAM_TWO_BEGIN_TIME").getValue();
        this.provincialExamEndTime = activityConfigMap.get("STUDY_ROOM_EXAM_TWO_END_TIME").getValue();
        this.palaceExamBeginTime = activityConfigMap.get("STUDY_ROOM_EXAM_THREE_BEGIN_TIME").getValue();
        this.palaceExamEndTime = activityConfigMap.get("STUDY_ROOM_EXAM_THREE_END_TIME").getValue();
        this.provincialExamNeedRanking = activityConfigMap.get("STUDY_ROOM_EXAM_PROVINCIAL_QUALIFICATION").getIntValue();
        this.palaceExamNeedRanking  =   activityConfigMap.get("STUDY_ROOM_EXAM_PALACE_QUALIFICATION").getIntValue();

        this.localExamCanJoinNum = activityConfigMap.get("STUDY_ROOM_EXAM_LOCAL_FREQUENCY").getIntValue();
        this.provincialExamCanJoinNum = activityConfigMap.get("STUDY_ROOM_EXAM_PROVINCIAL_FREQUENCY").getIntValue();
        this.palaceExamCanJoinNum = activityConfigMap.get("STUDY_ROOM_EXAM_PALACE_FREQUENCY").getIntValue();

        this.shareReward = activityConfigMap.get("STUDY_ROOM_SHARE_REWARD").getValue();

        this.joinAiTime = activityConfigMap.get("STUDY_ROOM_EXAM_AI_JOIN_TIME").getIntValue();

        this.mailTitle  = activityConfigMap.get("STUDY_ROOM_GOODS_AUTO_CONSUME_TITLE").getValue();

        this.mailContent = activityConfigMap.get("STUDY_ROOM_GOODS_AUTO_CONSUME_CONTENT").getValue();

        this.sitDownRate = activityConfigMap.get("STUDY_ROOM_LOCK_PROBABILITY").getIntValue();

        String headIconString = "102110|102120|102130|102140|102150|102210|102220|102230|102240|102250|102211|102221|102231|102251|102212|102222|102232|102242|102252|102310|102320|102330|102311|102321|102331|102341|102351|102312|102322|102332|102342|102430|102411|102421|102431|102441|102451|102412|102422|102432|102442|102452|102413|102423|102433|102443|102453|102414|102424|102444|102454|102415|102425|102435|102445|102455|102416|102426|102436|102456|102417|103000|103002|103003|103004|103100|103101|103102|103103|103104|103105|103106|103108|103109|103110|103111|103200|103201|103202|103203|103204|103205|103206|103207|103208|103209|103210|103212|103213|103300|103301|103302|103303|103304|103305|103306|103307|103308|103309|103310|103311|103312|103317|103318|103320|103322|103323|103326";

        //获取配置的头像 兼容迭代
        if (activityConfigMap.containsKey("STUDY_ROOM_AI_HEAD_ICON_ID")){
            headIconString = activityConfigMap.get("STUDY_ROOM_AI_HEAD_ICON_ID").getValue();
        }

        headIconList = StringUtils.stringToIntegerList(headIconString, "\\|");
        try {
            this.examBeginDate = getDateByConfig(this.examBeginTime);
            this.examEndDate = getDateByConfig(this.examEndTime);
            this.localExamBeginDate = getDateByConfig(this.localExamBeginTime);
            this.localExamEndDate = getDateByConfig(this.localExamEndTime);
            this.provincialExamBeginDate = getDateByConfig(this.provincialExamBeginTime);
            this.provincialExamEndDate = getDateByConfig(this.provincialExamEndTime);
            this.palaceExamBeginDate = getDateByConfig(this.palaceExamBeginTime);
            this.palaceExamEndDate = getDateByConfig(this.palaceExamEndTime);

            for (FlowerStudyExamConfig examConfig:examConfigMap.values()){
                examConfig.setStartDate(getDateByConfig(examConfig.getStartTime()));
                examConfig.setEndDate(getDateByConfig(examConfig.getEndTime()));
            }


        }catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

    public List<Integer> getHeadIconList() {
        return headIconList;
    }

    public int getSitDownRate() {
        return sitDownRate;
    }

    public String getPoemBeginTime() {
        return poemBeginTime;
    }

    public String getPoemEndTime() {
        return poemEndTime;
    }

    public int getPoemQANum() {
        return poemQANum;
    }

    public String getExamBeginTime() {
        return examBeginTime;
    }

    public String getExamEndTime() {
        return examEndTime;
    }

    public String getLocalExamBeginTime() {
        return localExamBeginTime;
    }

    public String getLocalExamEndTime() {
        return localExamEndTime;
    }

    public String getProvincialExamBeginTime() {
        return provincialExamBeginTime;
    }

    public String getProvincialExamEndTime() {
        return provincialExamEndTime;
    }

    public String getPalaceExamBeginTime() {
        return palaceExamBeginTime;
    }

    public String getPalaceExamEndTime() {
        return palaceExamEndTime;
    }

    public int getProvincialExamNeedRanking() {
        return provincialExamNeedRanking;
    }

    public int getPalaceExamNeedRanking() {
        return palaceExamNeedRanking;
    }

    public int getLocalExamCanJoinNum() {
        return localExamCanJoinNum;
    }

    public int getProvincialExamCanJoinNum() {
        return provincialExamCanJoinNum;
    }

    public int getPalaceExamCanJoinNum() {
        return palaceExamCanJoinNum;
    }

    public int getExamRoomPlayerNum() {
        return examRoomPlayerNum;
    }

    public int getLocalExamQuestionNum() {
        return localExamQuestionNum;
    }

    public int getProvincialExamQuestionNum() {
        return provincialExamQuestionNum;
    }

    public int getPalaceExamQuestionNum() {
        return palaceExamQuestionNum;
    }

    public int getAiAccuracy() {
        return aiAccuracy;
    }

    public int getAnswerTime() {
        return answerTime;
    }

    public int getDetermineTime() {
        return determineTime;
    }

    public int getExamOneQuestionScore() {
        return examOneQuestionScore;
    }

    public Date getExamBeginDate() {
        return examBeginDate;
    }

    public Date getExamEndDate() {
        return examEndDate;
    }

    public Date getLocalExamBeginDate() {
        return localExamBeginDate;
    }

    public Date getLocalExamEndDate() {
        return localExamEndDate;
    }

    public Date getProvincialExamBeginDate() {
        return provincialExamBeginDate;
    }

    public Date getProvincialExamEndDate() {
        return provincialExamEndDate;
    }

    public Date getPalaceExamBeginDate() {
        return palaceExamBeginDate;
    }

    public Date getPalaceExamEndDate() {
        return palaceExamEndDate;
    }

    /**
     * 检测诗书问答是否开启
     * @param date
     * @return
     * @throws ParseException
     */
    public boolean checkPoemOpen(Date date) throws ParseException {
        Date beginDate = getDateByConfig(poemBeginTime);
        Date endDate = getDateByConfig(poemEndTime);
        if (date.after(beginDate) && date.before(endDate)) {
            return true;
        }
        return false;
    }



    /**
     * 检测一考到底是否开启
     */
    public boolean checkExamOpen(Date date) throws ParseException {
        Date beginDate = getDateByConfig(examBeginTime);
        Date endDate = getDateByConfig(examEndTime);
        if (date.after(beginDate) && date.before(endDate)) {
            return true;
        }
        return false;
    }

    /**
     * 检测本地考试是否开启
     */
    public boolean checkLocalExamOpen(Date date) throws ParseException {
        Date beginDate = getDateByConfig(localExamBeginTime);
        Date endDate = getDateByConfig(localExamEndTime);
        if (date.after(beginDate) && date.before(endDate)) {
            return true;
        }
        return false;
    }

    /**
     * 检测省考是否开启
     */
    public boolean checkProvinceExamOpen(Date date) throws ParseException {
        Date beginDate = getDateByConfig(provincialExamBeginTime);
        Date endDate = getDateByConfig(provincialExamEndTime);
        if (date.after(beginDate) && date.before(endDate)) {
            return true;
        }
        return false;
    }

    /**
     * 检测殿考是否开启
     */
    public boolean checkPalaceExamOpen(Date date) throws ParseException {
        Date beginDate = getDateByConfig(palaceExamBeginTime);
        Date endDate = getDateByConfig(palaceExamEndTime);
        if (date.after(beginDate) && date.before(endDate)) {
            return true;
        }
        return false;
    }

    /**
     * @param configString
     * 获取时间 =
     * @return
     * @throws ParseException
     */
    public Date getDateByConfig(String configString) throws ParseException {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy=MM=dd=HH=mm");
            Date beginShowTime = format.parse(configString);
            return beginShowTime;
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 获取当前一答到底进行中游戏的场次id
     */
    public int getExamStatus(){
        Date now = new Date();
        for (FlowerStudyExamConfig examConfig:examConfigMap.values()){
            if (examConfig.getStartDate().before(now) && examConfig.getEndDate().after(now)){
                return examConfig.getId();
            }
        }
        return 0;
    }

    /**
     * 获取当前一答到底大场次类型
     */
    public int getExamNowPeriod(){
        Date now = new Date();
        if (now.before(getExamBeginDate())) {
            return eFlowerStudyPeriodType.noOpenLocal.getValue();
        }
        if (now.after(getExamEndDate())) {
            return eFlowerStudyPeriodType.allEnd.getValue();
        }

        if (now.before(getLocalExamBeginDate())) {
            return eFlowerStudyPeriodType.noOpenLocal.getValue();
        }
        if (now.after(getLocalExamBeginDate()) && now.before(getLocalExamEndDate())) {
            return eFlowerStudyPeriodType.openLocal.getValue();
        }
        if (now.after(getLocalExamEndDate()) && now.before(getProvincialExamBeginDate())) {
            return eFlowerStudyPeriodType.localToProvincial.getValue();
        }
        if (now.after(getProvincialExamBeginDate()) && now.before(getProvincialExamEndDate())) {
            return eFlowerStudyPeriodType.openProvincial.getValue();
        }
        if (now.after(getProvincialExamEndDate()) && now.before(getPalaceExamBeginDate())) {
            return eFlowerStudyPeriodType.provincialToPalace.getValue();
        }
        if (now.after(getPalaceExamBeginDate()) && now.before(getPalaceExamEndDate())) {
            return eFlowerStudyPeriodType.openPalace.getValue();
        }
        if (now.after(getPalaceExamEndDate())) {
            return eFlowerStudyPeriodType.allEnd.getValue();
        }
        return eFlowerStudyPeriodType.noOpenLocal.getValue();
    }

    public int getRandomHead(){
        if (headIconList == null || headIconList.size() == 0){
            return 106000;
        }

        Random random = new Random();
        int index = random.nextInt(headIconList.size());
        return headIconList.get(index);
    }


}
